package com.jhanlos.flights.objects;

import java.awt.Color;
import javax.media.opengl.GL;
import com.jhanlos.flights.worldwind.geom.AirportObject;
import gov.nasa.worldwind.globes.Globe;
import gov.nasa.worldwind.render.DrawContext;
import gov.nasa.worldwind.render.Renderable;
import gov.nasa.worldwind.util.Logging;

public class FlightsModelRenderable implements Renderable{
	
	Flights flights;
	Globe globe;
	AirportObject airportObject;
	public FlightsModelRenderable(Flights flights,Globe globe) {
	
		this.flights = flights;
		this.globe = globe;
		
		airportObject = new AirportObject(0, 0, 0,200, "No Name", globe);
		
	}
	@Override
	public void render(DrawContext dc) {
		 if (dc == null)
	        {
	            String msg = Logging.getMessage("nullValue.DrawContextIsNull");
	            Logging.logger().severe(msg);
	            throw new IllegalArgumentException(msg);
	        }
	        
	        javax.media.opengl.GL gl = dc.getGL();
	        
//	        gl.glEnable(GL.GL_TEXTURE_2D); // removed - sun shading disabeled below
	        gl.glPushAttrib(javax.media.opengl.GL.GL_TEXTURE_BIT | javax.media.opengl.GL.GL_ENABLE_BIT | javax.media.opengl.GL.GL_CURRENT_BIT);
	        gl.glMatrixMode(javax.media.opengl.GL.GL_MODELVIEW);

	        // Added so that the colors wouldn't depend on sun shading
	        gl.glDisable(GL.GL_TEXTURE_2D);
	        Color airColor = new Color(1.0f,0,0);
	        gl.glColor3f( airColor.getRed()/255.0f , airColor.getGreen()/255.0f , airColor.getBlue()/255.0f ); // COLOR
            
            gl.glBegin(GL.GL_LINE_STRIP);
            
	        int numflight = flights.getNumFlights();
	        
	        for (int i = 0; i < numflight; i++) {
	        	
	            double[] xyz = flights.getFlight(i);
	            gl.glVertex3f((float)xyz[0], (float)xyz[1], (float)xyz[2]);
	           
			}
	        gl.glEnd();
	        
	        Airport airports[] = flights.getAirports();
	        
	        
	        for (int i = 0; i < airports.length; i++) {
	        	airportObject.setName(airports[i].getName());
	        	airportObject.setLastLat(airports[i].getLat());
	        	airportObject.setLastLon(airports[i].getLog());
	        	airportObject.setLastAlt(airports[i].getAlt());
	        	airportObject.render(dc);
	        	
			}
	        
	        gl.glPopAttrib();
		
	}

}
